﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using GC2.Bussines;
using GC2.Bussines.BMAdmin;
using GC2.Entities.Admin;
using GC2.ExceptionHandler;
using GC2.Funciones.General;

public partial class StockGestion : GenericPage
{
    FunctionsSite funciones = new FunctionsSite();
    private Stock stock = new Stock();    
    private ExceptionConstructor exNew = new ExceptionConstructor();
    private int Accion;

    #region Controles

    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            SetearCultura();
            SetearConexion();
            if (!this.IsPostBack)
            {
                Accion = Convert.ToInt16(Session["Accion"].ToString());
                stock = (Stock)Session["Stock"];
                btnLotes.Visible = false;
                lblAutoLote.Visible = false;
                if (Accion > 1)
                {
                    HidrataControles(stock);
                    if (Accion == 2)
                    {
                        lblAccion.Text = "Modifica Datos";
                    }
                }
                else
                {
                    HidrataCabecera(stock);

                     // es un alta de articulo
                    txtPrecio.Text = "0";
                    txtSaldo.Text = "0";
                    btnLotes.Visible = false;
                    txtCodigo.ReadOnly = false;
                    txtCodigo.Text = "0";
                    txtPuntoPedido.ReadOnly = false;
                    txtPuntoPedido.Text = "0";

                    lblAccion.Text = "Agrega Nuevo Articulo";
                }
            }
        }
        catch (Exception ex)
        {
            ErrorUI(ex);
        }

    }
    #endregion 

    #region btnCancelar_Click
    protected void btnCancelar_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Seguridad/Default.aspx", false);
    }
    #endregion

    #region btnLotes_Click
    protected void btnLotes_Click(object sender, EventArgs e)
    {
        try
        {
            stock = (Stock)Session["Stock"];
            Session["Stock"] = stock;
            Session["Accion"] = 2;
            Response.Redirect("LotesAdmin.aspx", false);
        }
        catch (Exception ex)
        {
            ErrorUI(ex);
        }
    }
    #endregion

    #region btnGuardar_Click
    protected void btnGuardar_Click(object sender, EventArgs e)
    {
        try
        {
            BMStock bmStock = new BMStock(Connection);
            stock = (Stock)Session["Stock"];
            Accion = Convert.ToInt16(Session["Accion"].ToString());
            if (Accion == 1)
            {
                if (txtCodigo.Text.Trim().Length < 6)
                {
                    if (funciones.isNumeric(txtCodigo.Text.Trim()))
                    {
                        if (Convert.ToInt64(txtCodigo.Text.Trim()) > Int16.MaxValue)
                        {
                            lblMensaje.Text = "Código de Artículo debe ser menor a 32768.";
                            mpeMensaje.Show();
                            return;
                        }
                        Stock stockAux = stock;
                        stockAux.Id_Articulo = Convert.ToInt16(txtCodigo.Text.Trim());
                        stockAux = bmStock.BMStockGetOne(stockAux);
                        if (stockAux.Id_Articulo > 0)
                        {
                            lblMensaje.Text = "Código de Artículo YA Existe, verifique.";
                            mpeMensaje.Show();
                            return;
                        }
                        stock.Id_Articulo = Convert.ToInt16(txtCodigo.Text.Trim());
                    }
                    else
                    {
                        lblMensaje.Text = "Código de Artículo Incorrecto debe ser numérico.";
                        mpeMensaje.Show();
                        return;
                    }
                }
            }
            stock.Articulo = txtArticulo.Text;
            stock.CodigoBarra = txtCodBarra.Text;
            if (funciones.isNumeric(txtPrecio.Text.Trim()))
            {
                stock.Precio = Convert.ToDecimal(txtPrecio.Text.Trim());
            }
            else
            {
                lblMensaje.Text = "Precio sugerido Incorrecto.";
                mpeMensaje.Show();
                return;
            }
            if (funciones.isNumeric(txtSaldo.Text.Trim()))
            {
                stock.Saldo = Convert.ToDecimal(txtSaldo.Text.Trim());
            }
            else
            {
                lblMensaje.Text = "Saldo Incorrecto.";
                mpeMensaje.Show();
                return;
            }
            if (funciones.isNumeric(txtPuntoPedido.Text.Trim()))
            {
                stock.PuntoPedido = Convert.ToDecimal(txtPuntoPedido.Text.Trim());
            }
            else
            {
                lblMensaje.Text = "Punto de Pedido Incorrecto.";
                mpeMensaje.Show();
                return;
            }
            if (chkLote.Checked)
            {
                stock.UsaLotes = 1;
                if (chkAuto.Checked)
                {
                    stock.AutoLote = 1;
                }
                else
                {
                    stock.AutoLote = 0;
                }
            }
            else
            {
                stock.UsaLotes = 0;
                stock.AutoLote = 0;
            }

            if (txtCodBarra.Text != "")
            {
                if (funciones.isNumeric(txtLargoArt.Text.Trim()))
                {
                    stock.LargoArt = Convert.ToInt16(txtLargoArt.Text.Trim());
                }
                else
                {
                    lblMensaje.Text = "Largo Art. Incorrecto.";
                    mpeMensaje.Show();
                    return;
                }
            }


            
            if (Accion == 1)
            {
                InsertaStockNuevo(stock);
                lblAccion.Text = "Nuevo Artículo Agregado";
            }
            else
            {
                GuardaStock(stock);
                lblAccion.Text = "Artículo Actualizado";
            }
            btnGuardar.Enabled = false;
            if (chkLote.Checked)
            {
                btnLotes.Visible = true;
            }
        }
        catch (Exception ex)
        {
            ErrorUI(ex);
        }
    }
    #endregion

    #region chkLote_CheckedChanged
    protected void chkLote_CheckedChanged(object sender, EventArgs e)
    {
        if (chkLote.Checked)
        {
            btnLotes.Visible = true;
            lblAutoLote.Visible = true;
        }
        else
        {
            btnLotes.Visible = false;
            lblAutoLote.Visible = false;
        }
    }
    #endregion

    #endregion

    #region Métodos

    #region HidrataCabecera
    private void HidrataCabecera(Stock stock)
    {
        try
        {
            BMRubros bmRubro = new BMRubros(Connection);
            Rubros rubro = new Rubros();
            rubro.Id_Rubro = stock.Id_Rubro;
            rubro = bmRubro.BMRubrosSearch(rubro)[0];
            lblRubro.Text = "Rubro: " + rubro.Id_Rubro.ToString();
            txtRubro.Text = rubro.Rubro;

            BMSubRubros bmSubRubro = new BMSubRubros(Connection);
            SubRubros subRubro = new SubRubros();
            subRubro.Id_Rubro = stock.Id_Rubro;
            subRubro.Id_SubRubro = stock.Id_SubRubro;
            subRubro = bmSubRubro.BMSubRubrosSearch(subRubro)[0];
            lblSubRubro.Text = "SubRubro: " + subRubro.Id_SubRubro.ToString();
            txtSubRubro.Text = subRubro.SubRubro;
        }
        catch (Exception ex)
        {
            ErrorUI(ex);
        }
    }
    #endregion

    #region HidrataControles
    private void HidrataControles(Stock stock)
    {
        try
        {
            BMRubros bmRubro = new BMRubros(Connection);
            Rubros rubro = new Rubros();
            rubro.Id_Rubro = stock.Id_Rubro;
            rubro = bmRubro.BMRubrosSearch(rubro)[0];
            lblRubro.Text = "Rubro: " + rubro.Id_Rubro.ToString();
            txtRubro.Text = rubro.Rubro;

            BMSubRubros bmSubRubro = new BMSubRubros(Connection);
            SubRubros subRubro = new SubRubros();
            subRubro.Id_Rubro = stock.Id_Rubro;
            subRubro.Id_SubRubro = stock.Id_SubRubro;
            subRubro = bmSubRubro.BMSubRubrosSearch(subRubro)[0];
            lblSubRubro.Text = "SubRubro: " + subRubro.Id_SubRubro.ToString();
            txtSubRubro.Text = subRubro.SubRubro;

            txtCodigo.Text = stock.Id_Articulo.ToString();
            txtCodigo.ReadOnly = true;
            txtArticulo.Text = stock.Articulo.ToString();
            txtCodBarra.Text = stock.CodigoBarra.ToString();
            txtLargoArt.Text = stock.LargoArt.ToString();
            decimal dTemp = Math.Round(Convert.ToDecimal(stock.Precio),2);
            txtPrecio.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(stock.PuntoPedido), 2);
            txtPuntoPedido.Text = dTemp.ToString();
            dTemp = Math.Round(Convert.ToDecimal(stock.Saldo), 2);
            txtSaldo.Text = dTemp.ToString();
            if(stock.UsaLotes == 1)
            {
                chkLote.Checked = true;
                btnLotes.Visible = true;
                lblAutoLote.Visible = true;
                if (stock.AutoLote == 1)
                {
                    chkAuto.Checked = true;
                }
                else
                {
                    chkAuto.Checked = false;
                }
            }
        }
        catch (Exception ex)
        {
            ErrorUI(ex);
        }
    }
    #endregion

    #region GuardaStock
    private void GuardaStock(Stock oStock)
    {
        try
        {
            BMStock bmStock = new BMStock(Connection);
            bmStock.BMStockUpdate(oStock);
        }
        catch (Exception ex)
        {
            ErrorUI(ex);
        }
    }
    #endregion

    #region InsertaStock
    private void InsertaStock(Stock oStock)
    {
        try
        {
            oStock.Estado = 1;
            BMStock bmStock = new BMStock(Connection);
            bmStock.BMStockInsert(oStock);
        }
        catch (Exception ex)
        {
            ErrorUI(ex);
        }
    }
    #endregion

    #region InsertaStockNuevo
    private void InsertaStockNuevo(Stock oStock)
    {
        try
        {            
            oStock.Estado = 1;
            BMStock bmStock = new BMStock(Connection);
            bmStock.BMStockInsertNuevo(oStock);
        }
        catch (Exception ex)
        {
            ErrorUI(ex);
        }
    }
    #endregion

    #region ActualizaLotes
    private void ActualizaLotes(Stock oStock)
    {
        try
        {
            //BMLotes bmLotes = new BMLotes(Connection);
            //Lotes lote = new Lotes();

            //lote.Id_Rubro = oStock.Id_Rubro;
            //lote.Id_SubRubro = oStock.Id_SubRubro;
            //lote.Id_Articulo = oStock.Id_Articulo;
            //lote.Lote = oStock.Id_Lote;
            //lote.Estado = 1;

            //bmLotes.BMLotesUpdate(lote);
        }
        catch (Exception ex)
        {
            ErrorUI(ex);
        }
    }
    #endregion

    #endregion

}
